package com.zheng.study.base.bean.search.page;

import com.zheng.study.base.bean.search.SearchEnum;
import com.zheng.study.base.bean.search.projection.Projection;
import com.zheng.study.base.bean.search.projection.ProjectionHelp;
import org.springframework.util.Assert;

/**
 * 查询 - 参数 - PagePaging
 * ============================================================================
 * author : fallenpanda
 * createDate:  2018/6/11 。
 * ============================================================================
 */
public class PagePaging extends PageList {

	private static final Integer DEFAULT_PAGE_NUMBER = 1;
	private static final Integer DEFAULT_PAGE_SIZE = 20;
	private static final Integer MAX_PAGE_SIZE = 500;

	private final Integer pageNumber;
	private final Integer pageSize;

	public PagePaging(SearchEnum.ResultType resultType, Projection projection, Integer pageNumber, Integer pageSize) {
		super(resultType, projection);
		this.pageNumber = pageNumber;
		this.pageSize = pageSize;
	}

	public Integer getPageNumber() {
		return pageNumber;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public static PagePaging simplePage(){
		return newBuilder().setResultType(SearchEnum.ResultType.list).setProjection(ProjectionHelp.none()).setPageNumber(DEFAULT_PAGE_NUMBER).setPageSize(DEFAULT_PAGE_SIZE).build();
	}

	public static PagePaging page(Integer pageNumber, Integer pageSize){
		return newBuilder().setResultType(SearchEnum.ResultType.list).setProjection(ProjectionHelp.none()).setPageNumber(pageNumber).setPageSize((pageSize > MAX_PAGE_SIZE)?MAX_PAGE_SIZE:pageSize).build();
	}

	public static Builder newBuilder(){
		return new Builder();
	}

	public static class Builder extends PageList.Builder{

		private Integer pageNumber;
		private Integer pageSize;

		public Builder setPageNumber(Integer pageNumber) {
			this.pageNumber = pageNumber;
			return this;
		}

		public Builder setPageSize(Integer pageSize) {
			this.pageSize = pageSize;
			return this;
		}

		@Override
		protected Builder getThis() {
			return this;
		}

		@Override
		public Builder setResultType(SearchEnum.ResultType resultType) {
			this.resultType = resultType;
			return this;
		}

		@Override
		public Builder setProjection(Projection projection) {
			this.projection = projection;
			return this;
		}

		@Override
		public PagePaging build() {
			Assert.notNull(resultType, "resultType 不能为空");
			Assert.notNull(projection, "projection 不能为空");
			return new PagePaging(resultType, projection, pageNumber, pageSize);
		}

	}

}
